package cn.wsalix.observable.service.impl;

import java.util.Observable;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.apache.shiro.authc.UsernamePasswordToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.wsalix.admin.entity.SysUser;
import cn.wsalix.admin.form.UserForm;
import cn.wsalix.admin.service.UserService;
import cn.wsalix.exception.UserException;
import cn.wsalix.login.service.QQAccessTokenService;
import cn.wsalix.login.service.QQUserService;
import cn.wsalix.observable.service.LoginService;
import cn.wsalix.shiro.token.CaptchaToken;
import cn.wsalix.shiro.token.QQToken;
import cn.wsalix.shiro.token.ThirdToken;
import cn.wsalix.shiro.token.WxAppToken;
import cn.wsalix.user.entity.UserCenter;
import cn.wsalix.user.entity.UserInfo;
import cn.wsalix.user.service.UserCenterService;
import cn.wsalix.user.service.UserInfoService;
import cn.wsalix.weixin.service.WxUserService;

@Service("loginService")
public class LoginServiceImpl extends Observable implements LoginService {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private UserService userService;
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private UserCenterService userCenterService;
	private SysUser user;
	private UserInfo userInfo;
	private UserCenter userCenter;
	@Autowired
	private QQUserService qqUserService;
	@Autowired
	private WxUserService wxUserService;
	@Autowired
	private QQAccessTokenService qqAccessTokenService;
	@Autowired
	protected UserException userException;

	// private List<>
	@PostConstruct
	public void init() {
		logger.info("==========init");
		addObserver(userCenterService);
		addObserver(userInfoService);
		addObserver(userService);
		addObserver(qqUserService);
		addObserver(qqAccessTokenService);
		addObserver(wxUserService);
	}

	@PreDestroy
	public void destroy() {
		logger.info("==========destroy");
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public UserCenter getUserCenter() {
		return userCenter;
	}

	public void setUserCenter(UserCenter userCenter) {
		this.userCenter = userCenter;
	}

	public SysUser getUser() {
		return user;
	}

	public void setUser(SysUser user) {
		this.user = user;
	}

	@Transactional
	//@Override
	public UsernamePasswordToken login(UserForm form) {
		if (form.getRegType() == null) {
			if (form.getUsername() == null
					|| form.getUsername().trim().equals("")) {
				//throw Global
						//.getException(userException.getLoginUserNameError());
			}
			if (form.getPassword() == null
					|| form.getPassword().trim().equals("")) {
				//throw Global
					//	.getException(userException.getLoginPasswordError());
			}

			CaptchaToken token = new CaptchaToken();
			token.setUsername(form.getUsername());
			token.setPassword(form.getPassword().toCharArray());
			token.setRememberMe(true);
			setChanged();
			notifyObservers(token);
			return token;
		}
		switch (form.getRegType()) {
		case qq:
			QQToken upToken = new QQToken();
			upToken.setOpenId(form.getOpenid());
			upToken.setAccessToken(form.getAccessToken());
			upToken.setRememberMe(true);
			// upToken.setHost(host);
			upToken.setRemark(form.getRemark());

			setChanged();
			notifyObservers(upToken);
			return upToken;
		case sina:
			break;
		case weixin:// app端，通过AccessToken登录
			WxAppToken wxToken = new WxAppToken();
			wxToken.setOpenid(form.getOpenid());
			wxToken.setAccess_token(form.getAccessToken());
			wxToken.setRememberMe(true);
			// upToken.setHost(host);
			wxToken.setRemark(form.getRemark());

			setChanged();
			notifyObservers(wxToken);
			return wxToken;
		default:

		}
		return null;
	}

	@Transactional
	@Override
	public SysUser login(ThirdToken upToken) {
		setChanged();
		notifyObservers(upToken);
		return user;
	}

}
